package ui.global;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.graphics.Region;
import org.eclipse.swt.internal.win32.OS;
import org.eclipse.swt.widgets.Display;

//UI的工具类
public class GraphicsUtil {

	// 获取一个圆角矩形
	public static Region createRoundedRectangle(int x, int y, int W, int H, int r) {
		Region region = new Region();
		int d = (2 * r); // diameter

		region.add(circle(r, (x + r), (y + r)));
		region.add(circle(r, (x + W - r), (y + r)));
		region.add(circle(r, (x + W - r), (y + H - r)));
		region.add(circle(r, (x + r), (y + H - r)));
		
		if (H > 0 && W > 0) {
			region.add((x + r), y, (W - d), H);
			region.add(x, (y + r), W, (H - d));
		}
		return region;
	}

	// 获取一个圆角矩形区域
	private static int[] circle(int r, int offsetX, int offsetY) {
		int[] polygon = new int[8 * r + 4];
		// x^2 + y^2 = r^2
		for (int i = 0; i < 2 * r + 1; i++) {
			int x = i - r;
			int y = (int) Math.sqrt(r * r - x * x);
			polygon[2 * i] = offsetX + x;
			polygon[2 * i + 1] = offsetY + y;
			polygon[8 * r - 2 * i - 2] = offsetX + x;
			polygon[8 * r - 2 * i - 1] = offsetY - y;
		}
		return polygon;
	}

	// 由图片的透明度获取不规则的区域
	public static Region handleTransparenceRegion(Image image, int width, int height) {
		Region region = new Region();
		final ImageData imageData = image.getImageData();
		if (imageData.alphaData != null) {
			Rectangle pixel = new Rectangle(0, 0, 1, 1);
			for (int y = 0; y < imageData.height; y++) {
				for (int x = 0; x < imageData.width; x++) {
					if (imageData.getAlpha(x, y) == 0) {
						pixel.x = imageData.x + x;
						pixel.y = imageData.y + y;
						region.add(pixel);
					}
				}
			}
		}
		Region ret = new Region();
		ret.add(0, 0, width, height);
		ret.subtract(region);
		return ret;
	}

	// 设置成透明度
	public static void transparent(long handle, int hexColor, int transparency) {
		if (transparency >= 0) {
			transparency &= 0xff;
			OS.SetWindowLong(handle, OS.GWL_EXSTYLE, OS.WS_POPUP | OS.WS_EX_TRANSPARENT | OS.WS_EX_LAYERED);
			OS.SetWindowPos(handle, OS.HWND_TOPMOST, 0, 0, 0, 0, OS.SWP_NOMOVE | OS.SWP_NOSIZE);
			if (hexColor >= 0) {
				OS.SetLayeredWindowAttributes(handle, hexColor, (byte) transparency, OS.LWA_ALPHA | OS.LWA_COLORKEY);
			} else {
				OS.SetLayeredWindowAttributes(handle, hexColor, (byte) transparency, OS.LWA_ALPHA);
			}
		} else if (hexColor >= 0) {
			OS.SetWindowLong(handle, OS.GWL_EXSTYLE, OS.HWND_TOPMOST | OS.WS_EX_TRANSPARENT | OS.WS_EX_LAYERED);
			OS.SetLayeredWindowAttributes(handle, hexColor, (byte) transparency, OS.LWA_COLORKEY);
		}
	}

	/**
	 * 根据指定的宽高对{@link Image}图像进行绽放
	 * 
	 * @param src    原图对象
	 * @param width  目标图像宽度
	 * @param height 目标图像高度
	 * @return 返回缩放后的{@link Image}对象
	 */
	public static Image resize(Image src, int width, int height) {
		Image scaled = new Image(Display.getDefault(), width, height);
		GC gc = new GC(scaled);
		try {
			gc.setAdvanced(true); // 打开高级绘图模式
			gc.setAntialias(SWT.ON);// 设置消除锯齿
			gc.setInterpolation(SWT.HIGH); // 设置插值
			gc.drawImage(src, 0, 0, src.getBounds().width, src.getBounds().height, 0, 0, width, height);
		} finally {
			gc.dispose();
		}
		return scaled;
	}

}
